<%@ page import="com.alinu.settings.domain.DicValue" %>
<%@ page import="com.alinu.workbench.domain.Tran" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.Set" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
    List<DicValue> dicValues = (List<DicValue>) application.getAttribute("stage");
    Map<String, String> stage2Possibility = (Map<String, String>) application.getAttribute("stagePossibility");
    Set<String> keys = stage2Possibility.keySet();
    int point = 0;
    for (int i = 0; i < dicValues.size(); i++) {
        DicValue dicValue = dicValues.get(i);
        String stage = dicValue.getValue();
        String possibility = stage2Possibility.get(stage);
        if ("0".equals(possibility)) {
            point = i;
            break;
        }
    }

%>
<!DOCTYPE html>
<html>
<head>
    <base href="<%=basePath%>">
    <meta charset="UTF-8">

    <link href="jquery/bootstrap_3.3.0/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>

    <style type="text/css">
        .mystage {
            font-size: 20px;
            vertical-align: middle;
            cursor: pointer;
        }

        .closingDate {
            font-size: 15px;
            cursor: pointer;
            vertical-align: middle;
        }
    </style>

    <script src="jquery/jquery-1.11.1-min.js" type="text/javascript"></script>
    <script src="jquery/bootstrap_3.3.0/js/bootstrap.min.js" type="text/javascript"></script>

    <script type="text/javascript">

        //默认情况下取消和保存按钮是隐藏的
        var cancelAndSaveBtnDefault = true;

        $(function () {
            $("#remark").focus(function () {
                if (cancelAndSaveBtnDefault) {
                    //设置remarkDiv的高度为130px
                    $("#remarkDiv").css("height", "130px");
                    //显示
                    $("#cancelAndSaveBtn").show("2000");
                    cancelAndSaveBtnDefault = false;
                }
            });

            $("#cancelBtn").click(function () {
                //显示
                $("#cancelAndSaveBtn").hide();
                //设置remarkDiv的高度为130px
                $("#remarkDiv").css("height", "90px");
                cancelAndSaveBtnDefault = true;
            });

            $(".remarkDiv").mouseover(function () {
                $(this).children("div").children("div").show();
            });

            $(".remarkDiv").mouseout(function () {
                $(this).children("div").children("div").hide();
            });

            $(".myHref").mouseover(function () {
                $(this).children("span").css("color", "red");
            });

            $(".myHref").mouseout(function () {
                $(this).children("span").css("color", "#E6E6E6");
            });

            //阶段提示框
            $(".mystage").popover({
                trigger: 'manual',
                placement: 'bottom',
                html: 'true',
                animation: false
            }).on("mouseenter", function () {
                var _this = this;
                $(this).popover("show");
                $(this).siblings(".popover").on("mouseleave", function () {
                    $(_this).popover('hide');
                });
            }).on("mouseleave", function () {
                var _this = this;
                setTimeout(function () {
                    if (!$(".popover:hover").length) {
                        $(_this).popover("hide")
                    }
                }, 100);
            });

            let tranId;
            tranId = $.trim($("#tranId").html());
            // 查询交易的交易历史
            getTranHistoryList(tranId);

        });

        function getTranHistoryList(tranId) {
            $.ajax({
                url: "workbench/transaction/getTranHistoryList.do",
                data: {
                    "tranId": tranId
                },
                type: "get",
                datatype: "json",
                success: function (data) {
                    let html = "";
                    let dataString = JSON.parse(data);
                    $.each(dataString, function (index, item) {
                        html += '<tr><td>' + item.stage + '</td>';
                        html += '<td>' + item.money + '</td>';
                        html += '<td>' + item.possibility + '</td>';
                        html += '<td>' + item.expectedDate + '</td>';
                        html += '<td>' + item.createTime + '</td>';
                        html += '<td>' + item.createBy + '</td></tr>';
                    });
                    $("#tranHistoryBody").html(html);
                },
                error: function () {
                    alert("服务异常，查询历史列表失败");
                }
            });
        }

        function changeStage(stage, i) {
            $.ajax({
                url: "workbench/transaction/changeStage.do",
                data: {
                    "id": "${tran.id}",
                    "stage": stage,
                    "money": "${tran.money}",
                    "expectedDate": "${tran.expectedDate}"
                },
                type: "post",
                datatype: "json",
                success: function (data) {
                    // 改变阶段成功后，要刷新页面信息
                    data = JSON.parse(data);
                    if (data.result) {
                        $("#stage").html(data.tran.stage);
                        $("#possibility").html(data.tran.possibility);
                        console.log(data.tran.possibility);
                        $("#editBy").html(data.tran.editBy);
                        $("#editTime").html(data.tran.editTime);

                        // 改变阶段成功后，将阶段图标更新，阶段历史更新
                        updateIcons(stage, i);
                        getTranHistoryList(data.tran.id);
                    }
                },
                error: function () {
                    alert("切换交易阶段失败");
                }
            });
        }

        function updateIcons(stage, index) {
            // 当前阶段和当前可能性
            let currentStage = stage;
            let currentPossibility = $("#possibility").html();

            // 分界点下标
            let point = "<%=point%>";

            // 如果当前阶段可能性为0，前七黑，后一个红叉，一个黑叉
            if (currentPossibility === "0") {
                for (let i = 0; i < point; i++) {
                    $("#" + i).removeClass();
                    $("#" + i).addClass("glyphicon glyphicon-record mystage");
                    $("#" + i).css("color", "#000000");
                }

                for (let i = point; i < <%=dicValues.size()%>; i++) {
                    // 当前阶段
                    if (i !== index) {
                        $("#" + i).removeClass();
                        $("#" + i).addClass("glyphicon glyphicon-remove mystage");
                        $("#" + i).css("color", "#000000");
                    } else {
                        $("#" + i).removeClass();
                        $("#" + i).addClass("glyphicon glyphicon-remove mystage");
                        $("#" + i).css("color", "#FF0000");
                    }
                }

            } else {
                // 当前阶段可能性不为0，则后俩为黑叉，前七个不确定
                for (let i = 0; i < point; i++) {
                    if (i < index) {
                        $("#" + i).removeClass();
                        $("#" + i).addClass("glyphicon glyphicon-ok-circle mystage");
                        $("#" + i).css("color", "#90F790");
                    } else if (i > index) {
                        $("#" + i).removeClass();
                        $("#" + i).addClass("glyphicon glyphicon-record mystage");
                        $("#" + i).css("color", "#000000");
                    } else {
                        $("#" + i).removeClass();
                        $("#" + i).addClass("glyphicon glyphicon-map-marker mystage");
                        $("#" + i).css("color", "#90F790");
                    }
                }

                for (let i = point; i < <%=dicValues.size()%>; i++) {
                    //黑叉
                    $("#" + i).removeClass();
                    $("#" + i).addClass("glyphicon glyphicon-remove mystage");
                    $("#" + i).css("color", "#000000");
                }
            }


        }


    </script>

</head>
<body>

<!-- 返回按钮 -->
<div style="position: relative; top: 35px; left: 10px;">
    <a href="javascript:void(0);" onclick="window.history.back();"><span class="glyphicon glyphicon-arrow-left"
                                                                         style="font-size: 20px; color: #DDDDDD"></span></a>
</div>

<!-- 大标题 -->
<div style="position: relative; left: 40px; top: -30px;">
    <div class="page-header">
        <h3>${tran.customerId}-${tran.name} <small>￥${tran.money}</small></h3>
    </div>
    <div style="position: relative; height: 50px; width: 250px;  top: -72px; left: 700px;">
        <button class="btn btn-default" onclick="window.location.href='edit.html';" type="button"><span
                class="glyphicon glyphicon-edit"></span> 编辑
        </button>
        <button class="btn btn-danger" type="button"><span class="glyphicon glyphicon-minus"></span> 删除</button>
    </div>
</div>

<!-- 阶段状态 -->
<div style="position: relative; left: 40px; top: -50px;">
    阶段&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <%
        // 准备当前阶段
        Tran tran = (Tran) request.getAttribute("tran");
        String currentStage = tran.getStage();
        // 准备当前阶段的可能性
        String currentPossibility = stage2Possibility.get(currentStage);

        // 当前阶段可能性为0，前七个为黑圈，后两个一个红叉，一个黑叉
        if ("0".equals(currentPossibility)) {
            for (int i = 0; i < dicValues.size(); i++) {
                // 遍历出的阶段，获得可能性
                DicValue dicValue = dicValues.get(i);
                String listStage = dicValue.getValue();
                String listPossibility = stage2Possibility.get(listStage);
                // 如果遍历的可能性为0
                if ("0".equals(listPossibility)) {
                    // 如果是当前阶段
                    if (listStage.equals(currentStage)) {
                        // 红叉
    %>
    <span id="<%=i%>" onclick="changeStage('<%=listStage%>', '<%=i%>')" class="glyphicon glyphicon-remove mystage"
          data-toggle="popover" data-placement="bottom" data-content="<%=dicValue.getText()%>"
          style="color: #FF0000;"></span>
    -----------
    <%
    } else {
        // 黑叉
    %>
    <span id="<%=i%>" onclick="changeStage('<%=listStage%>', '<%=i%>')" class="glyphicon glyphicon-remove mystage"
          data-toggle="popover" data-placement="bottom" data-content="<%=dicValue.getText()%>"
          style="color: #000000;"></span>
    -----------
    <%
        }

    } else {
        // 前七个全黑圈
    %>
    <span id="<%=i%>" onclick="changeStage('<%=listStage%>', '<%=i%>')" class="glyphicon glyphicon-record mystage"
          data-toggle="popover" data-placement="bottom" data-content="<%=dicValue.getText()%>"
          style="color: #000000;"></span>
    -----------
    <%
            }
        }
        // 如果当前阶段的可能性不为0，前七个有可能为黑圈，绿色标记，绿圈，后两个一定为黑叉
    } else {
        int index = 0;
        for (int i = 0; i < dicValues.size(); i++) {
            DicValue dicValue = dicValues.get(i);
            String stage = dicValue.getValue();
            // 是当前阶段
            if (stage.equals(currentStage)) {
                index = i;
                break;
            }
        }
        for (int i = 0; i < dicValues.size(); i++) {
            // 遍历出的阶段，获得可能性
            DicValue dicValue = dicValues.get(i);
            String listStage = dicValue.getValue();
            String listPossibility = stage2Possibility.get(listStage);

            // 如果遍历出的阶段可能性为0,说明是后两个阶段
            if ("0".equals(listPossibility)) {
                // 黑叉
    %>
    <span id="<%=i%>" onclick="changeStage('<%=listStage%>', '<%=i%>')" class="glyphicon glyphicon-remove mystage"
          data-toggle="popover" data-placement="bottom" data-content="<%=dicValue.getText()%>"
          style="color: #000000;"></span>
    -----------
    <%
    } else {
        // 如果是当前阶段，绿色标记
        if (i == index) {
    %>
    <span id="<%=i%>" onclick="changeStage('<%=listStage%>', '<%=i%>')" class="glyphicon glyphicon-map-marker mystage"
          data-toggle="popover" data-placement="bottom" data-content="<%=dicValue.getText()%>"
          style="color: #90F790;"></span>
    -----------
    <%
        // 小于当前阶段，绿圈
    } else if (i < index) {
    %>
    <span id="<%=i%>" onclick="changeStage('<%=listStage%>', '<%=i%>')" class="glyphicon glyphicon-record mystage"
          data-toggle="popover" data-placement="bottom" data-content="<%=dicValue.getText()%>"
          style="color: #90F790;"></span>
    -----------
    <%
        // 大于当前阶段，黑圈
    } else {
    %>
    <span id="<%=i%>" onclick="changeStage('<%=listStage%>', '<%=i%>')" class="glyphicon glyphicon-record mystage"
          data-toggle="popover" data-placement="bottom" data-content="<%=dicValue.getText()%>"
          style="color: #000000;"></span>
    -----------
    <%
                    }
                }
            }

        }


    %>
    <span class="closingDate">${tran.expectedDate}</span>
</div>

<!-- 详细信息 -->
<div style="position: relative; top: 0px;">
    <div hidden id="tranId">${tran.id}</div>
    <div style="position: relative; left: 40px; height: 30px;">
        <div style="width: 300px; color: gray;">所有者</div>
        <div style="width: 300px;position: relative; left: 200px; top: -20px;"><b>${tran.owner}</b></div>
        <div style="width: 300px;position: relative; left: 450px; top: -40px; color: gray;">金额</div>
        <div style="width: 300px;position: relative; left: 650px; top: -60px;"><b>${tran.money}</b></div>
        <div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px;"></div>
        <div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px; left: 450px;"></div>
    </div>
    <div style="position: relative; left: 40px; height: 30px; top: 10px;">
        <div style="width: 300px; color: gray;">名称</div>
        <div style="width: 300px;position: relative; left: 200px; top: -20px;"><b>${tran.customerId}-${tran.name}</b>
        </div>
        <div style="width: 300px;position: relative; left: 450px; top: -40px; color: gray;">预计成交日期</div>
        <div style="width: 300px;position: relative; left: 650px; top: -60px;"><b>${tran.expectedDate}</b></div>
        <div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px;"></div>
        <div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px; left: 450px;"></div>
    </div>
    <div style="position: relative; left: 40px; height: 30px; top: 20px;">
        <div style="width: 300px; color: gray;">客户名称</div>
        <div style="width: 300px;position: relative; left: 200px; top: -20px;"><b>${tran.customerId}</b></div>
        <div style="width: 300px;position: relative; left: 450px; top: -40px; color: gray;">阶段</div>
        <div style="width: 300px;position: relative; left: 650px; top: -60px;"><b id="stage">${tran.stage}</b></div>
        <div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px;"></div>
        <div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px; left: 450px;"></div>
    </div>
    <div style="position: relative; left: 40px; height: 30px; top: 30px;">
        <div style="width: 300px; color: gray;">类型</div>
        <div style="width: 300px;position: relative; left: 200px; top: -20px;"><b>${tran.type}</b></div>
        <div style="width: 300px;position: relative; left: 450px; top: -40px; color: gray;">可能性</div>
        <div style="width: 300px;position: relative; left: 650px; top: -60px;"><b
                id="possibility">${tran.possibility}</b></div>
        <div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px;"></div>
        <div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px; left: 450px;"></div>
    </div>
    <div style="position: relative; left: 40px; height: 30px; top: 40px;">
        <div style="width: 300px; color: gray;">来源</div>
        <div style="width: 300px;position: relative; left: 200px; top: -20px;"><b>${tran.source}</b></div>
        <div style="width: 300px;position: relative; left: 450px; top: -40px; color: gray;">市场活动源</div>
        <div style="width: 300px;position: relative; left: 650px; top: -60px;"><b>${tran.activityId}</b></div>
        <div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px;"></div>
        <div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px; left: 450px;"></div>
    </div>
    <div style="position: relative; left: 40px; height: 30px; top: 50px;">
        <div style="width: 300px; color: gray;">联系人名称</div>
        <div style="width: 500px;position: relative; left: 200px; top: -20px;"><b>${tran.contactsId}</b></div>
        <div style="height: 1px; width: 550px; background: #D5D5D5; position: relative; top: -20px;"></div>
    </div>
    <div style="position: relative; left: 40px; height: 30px; top: 60px;">
        <div style="width: 300px; color: gray;">创建者</div>
        <div style="width: 500px;position: relative; left: 200px; top: -20px;"><b>${tran.createBy}&nbsp;&nbsp;</b><small
                style="font-size: 10px; color: gray;">${tran.createTime}</small></div>
        <div style="height: 1px; width: 550px; background: #D5D5D5; position: relative; top: -20px;"></div>
    </div>
    <div style="position: relative; left: 40px; height: 30px; top: 70px;">
        <div style="width: 300px; color: gray;">修改者</div>
        <div style="width: 500px;position: relative; left: 200px; top: -20px;"><b
                id="editBy">${tran.editBy}&nbsp;&nbsp;</b><small
                style="font-size: 10px; color: gray;" id="editTime">${tran.editTime}</small></div>
        <div style="height: 1px; width: 550px; background: #D5D5D5; position: relative; top: -20px;"></div>
    </div>
    <div style="position: relative; left: 40px; height: 30px; top: 80px;">
        <div style="width: 300px; color: gray;">描述</div>
        <div style="width: 630px;position: relative; left: 200px; top: -20px;">
            <b>
                ${tran.description}
            </b>
        </div>
        <div style="height: 1px; width: 850px; background: #D5D5D5; position: relative; top: -20px;"></div>
    </div>
    <div style="position: relative; left: 40px; height: 30px; top: 90px;">
        <div style="width: 300px; color: gray;">联系纪要</div>
        <div style="width: 630px;position: relative; left: 200px; top: -20px;">
            <b>
                ${tran.contactSummary}&nbsp;
            </b>
        </div>
        <div style="height: 1px; width: 850px; background: #D5D5D5; position: relative; top: -20px;"></div>
    </div>
    <div style="position: relative; left: 40px; height: 30px; top: 100px;">
        <div style="width: 300px; color: gray;">下次联系时间</div>
        <div style="width: 500px;position: relative; left: 200px; top: -20px;"><b>&nbsp;${tran.nextContactTime}</b>
        </div>
        <div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -20px;"></div>
    </div>
</div>

<!-- 备注 -->
<div style="position: relative; top: 100px; left: 40px;">
    <div class="page-header">
        <h4>备注</h4>
    </div>

    <!-- 备注1 -->
    <div class="remarkDiv" style="height: 60px;">
        <img src="image/user-thumbnail.png" style="width: 30px; height:30px;" title="zhangsan">
        <div style="position: relative; top: -40px; left: 40px;">
            <h5>哎呦！</h5>
            <font color="gray">交易</font> <font color="gray">-</font> <b>动力节点-交易01</b> <small style="color: gray;">
            2017-01-22 10:10:10 由zhangsan</small>
            <div style="position: relative; left: 500px; top: -30px; height: 30px; width: 100px; display: none;">
                <a class="myHref" href="javascript:void(0);"><span class="glyphicon glyphicon-edit"
                                                                   style="font-size: 20px; color: #E6E6E6;"></span></a>
                &nbsp;&nbsp;&nbsp;&nbsp;
                <a class="myHref" href="javascript:void(0);"><span class="glyphicon glyphicon-remove"
                                                                   style="font-size: 20px; color: #E6E6E6;"></span></a>
            </div>
        </div>
    </div>

    <!-- 备注2 -->
    <div class="remarkDiv" style="height: 60px;">
        <img src="image/user-thumbnail.png" style="width: 30px; height:30px;" title="zhangsan">
        <div style="position: relative; top: -40px; left: 40px;">
            <h5>呵呵！</h5>
            <font color="gray">交易</font> <font color="gray">-</font> <b>动力节点-交易01</b> <small style="color: gray;">
            2017-01-22 10:20:10 由zhangsan</small>
            <div style="position: relative; left: 500px; top: -30px; height: 30px; width: 100px; display: none;">
                <a class="myHref" href="javascript:void(0);"><span class="glyphicon glyphicon-edit"
                                                                   style="font-size: 20px; color: #E6E6E6;"></span></a>
                &nbsp;&nbsp;&nbsp;&nbsp;
                <a class="myHref" href="javascript:void(0);"><span class="glyphicon glyphicon-remove"
                                                                   style="font-size: 20px; color: #E6E6E6;"></span></a>
            </div>
        </div>
    </div>

    <div id="remarkDiv" style="background-color: #E6E6E6; width: 870px; height: 90px;">
        <form role="form" style="position: relative;top: 10px; left: 10px;">
            <textarea class="form-control" id="remark" placeholder="添加备注..." rows="2"
                      style="width: 850px; resize : none;"></textarea>
            <p id="cancelAndSaveBtn" style="position: relative;left: 737px; top: 10px; display: none;">
                <button class="btn btn-default" id="cancelBtn" type="button">取消</button>
                <button class="btn btn-primary" type="button">保存</button>
            </p>
        </form>
    </div>
</div>

<!-- 阶段历史 -->
<div>
    <div style="position: relative; top: 100px; left: 40px;">
        <div class="page-header">
            <h4>阶段历史</h4>
        </div>
        <div style="position: relative;top: 0px;">
            <table class="table table-hover" id="activityTable" style="width: 900px;">
                <thead>
                <tr style="color: #B3B3B3;">
                    <td>阶段</td>
                    <td>金额</td>
                    <td>可能性</td>
                    <td>预计成交日期</td>
                    <td>创建时间</td>
                    <td>创建人</td>
                </tr>
                </thead>
                <tbody id="tranHistoryBody">
                </tbody>
            </table>
        </div>

    </div>
</div>

<div style="height: 200px;"></div>

</body>
</html>